package com.zhouzi.zhaoyang.ui.activity

import android.content.Intent
import android.os.Build
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.orhanobut.logger.Logger
import com.zhouzi.zhaoyang.R
import com.zhouzi.zhaoyang.adapter.rvadapter.CloseAccountAdapter
import com.zhouzi.zhaoyang.base.BaseActivity
import com.zhouzi.zhaoyang.bean.*
import com.zhouzi.zhaoyang.mvp.p.CloseAccountActivityPresenter
import com.zhouzi.zhaoyang.mvp.v.ICloseAccountActivityView
import com.zhouzi.zhaoyang.util.SPUtils
import com.zhouzi.zhaoyang.util.print.PrinterUtil
import kotlinx.android.synthetic.main.close_account_activity.*

class CloseAccountActivity : BaseActivity(),ICloseAccountActivityView {
    override fun errorToken() {
        exitToLogin()
    }

    override fun selectCloseAccountListSuccess(data :SelectCloseAccountListBean?) {
        smartRefresh.finishLoadMore(true)
        smartRefresh.finishRefresh(true)
        data.let {
            if (data!!.records.size == 0) {
                Toast.makeText(this, "暂无更多数据", Toast.LENGTH_SHORT).show()
            }else {
                smartRefresh.visibility = View.VISIBLE
                setRecyclerData(data)
            }
        }
    }

    private var adapter : CloseAccountAdapter?= null
    var closeAccountListBean  : SelectCloseAccountListBean?= null
    private fun setRecyclerData(data: SelectCloseAccountListBean?) {
        recyclerView.layoutManager = LinearLayoutManager(this) as RecyclerView.LayoutManager?

        if(loadMore && closeAccountListBean != null   ){
            data?.records?.addAll(closeAccountListBean!!.records)
        }
        adapter = CloseAccountAdapter(R.layout.close_account_rv, data!!.records)

        adapter?.setOnItemChildClickListener { adapter, view, position ->
            var bean = data.records[position]
            printBean(bean)
        }

        adapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, CloseAccountDetailActivity::class.java)
            intent.putExtra("data",data.records[position])
            startActivity(intent)
        }
        recyclerView.adapter = adapter
        closeAccountListBean = data

    }

    private fun printBean(data: SelectCloseAccountListBean.RecordsBean) {

        val wxDataFee = Gson().fromJson(data.wxDataFee,Bean2:: class.java)
        val aliDataFee = Gson().fromJson(data.aliDataFee, Bean1:: class.java)
        val memberDataFee = Gson().fromJson(data.memberDataFee,Bean3:: class.java)
        val tytDataFee = Gson().fromJson(data.tytDataFee,Bean4:: class.java)
        val sum = Gson().fromJson(data.sumDataFee,Bean5:: class.java)
        val refund = Gson().fromJson(data.refundDataFee,Bean6:: class.java)
        val deposit = Gson().fromJson(data.depositDataFee,DepositDataBean:: class.java)
        val coupon = Gson().fromJson(data.couponDataFee,CouponDataBean:: class.java)
        val settlementDataFee = Gson().fromJson(data.settlementDataFee,SettlementDataFee:: class.java)

        Logger.e(data.depositDataFee+data.couponDataFee)

        val util = PrinterUtil(this)
        var cbPrintSettings1: Int = SPUtils.getInstance().getValue("cbPrintSettings0", 1) as Int
        var cbPrintSettings2: Int = SPUtils.getInstance().getValue("cbPrintSettings1", 1) as Int
        var cbPrintSettings3: Int = SPUtils.getInstance().getValue("cbPrintSettings2", 1) as Int
        var cbPrintSettings4: Int = SPUtils.getInstance().getValue("cbPrintSettings3", 1) as Int
        var cbPrintSettings5: Int = SPUtils.getInstance().getValue("cbPrintSettings4", 1) as Int
        var cbPrintSettings6: Int = SPUtils.getInstance().getValue("cbPrintSettings5", 1) as Int
        var cbPrintSettings7: Int = SPUtils.getInstance().getValue("cbPrintSettings6", 1) as Int
        var cbPrintSettings8: Int = SPUtils.getInstance().getValue("cbPrintSettings7", 1) as Int
        var cbPrintSettings9: Int = SPUtils.getInstance().getValue("cbPrintSettings8", 1) as Int
        var line = ArrayList<String> ()
        line.add("交班结算")
        line.add("门店名称："+data?.storeName)
        line.add("结算店员："+data?.storeUserName)

      if(cbPrintSettings1 == 2){
          line.add("交易单数："+sum?.sumCount)
          line.add("交易金额："+sum?.sumFee)
      }
        if(cbPrintSettings2 == 2){

            if(!TextUtils.isEmpty(data?.settlementDataFee) && !"null".equals(data?.settlementDataFee)){
               line.add("实收金额：" + settlementDataFee?.settlementFee)
           }else{
               line.add("实收金额：0" )

           }
       }
        if (cbPrintSettings4 == 2){

            line.add("退款单数：" + refund?.refundCount)
         line.add("退款金额：" + refund?.refundFee)
     }
        if (cbPrintSettings3 == 2){

            if(!TextUtils.isEmpty(coupon?.couponTotalFee) && !"null".equals(coupon?.couponTotalFee)){
               line.add("优惠金额：" + coupon?.couponTotalFee)

           }else{
               line.add("优惠金额：0" )

           }
       }
        if (cbPrintSettings6 == 2){

            if(!TextUtils.isEmpty(deposit?.depositCount.toString()) && !"null".equals(deposit?.depositCount.toString())){
                line.add("押金单数：" + deposit?.depositCount)


            }else{
                line.add("押金单数：0" )

            }
            if(!TextUtils.isEmpty(deposit?.depositTotalFee) && !"null".equals(deposit?.depositTotalFee)){
                line.add("押金金额：" + deposit?.depositTotalFee)


            }else{
                line.add("押金金额：0" )

            }
        }

        if (cbPrintSettings7 == 2){

            line.add("微信单数："+wxDataFee?.wxCount)
          line.add("微信金额："+wxDataFee?.wxFee)
      }
        if (cbPrintSettings8 == 2){

            line.add("支付宝单数："+aliDataFee?.aliCount)
            line.add("支付宝金额："+aliDataFee?.aliFee)
        }

        if (cbPrintSettings5 == 2){

            line.add("会员单数："+memberDataFee?.memberCount)
          line.add("会员金额："+memberDataFee?.memberFee)
      }
        if (cbPrintSettings9 == 2){
            line.add("通用通单数："+tytDataFee?.tytCount)
            line.add("通用通金额："+tytDataFee?.tytFee)

        }


        if (!TextUtils.isEmpty(data?.beginTime) && !"null".equals(data?.beginTime)){
            line.add("开始时间："+data?.beginTime)
        }else{
            line.add("开始时间：")
        }

        line.add("结束时间："+data?.createdTime+"\n\n\n\n\n")


        util.createData(line)

    }

    override fun selectCloseAccountListFailed(msg: String) {
        smartRefresh.finishLoadMore(false)
        smartRefresh.finishRefresh(false)
        Toast.makeText(this,msg, Toast.LENGTH_SHORT)

    }

    var p = CloseAccountActivityPresenter()
    init {
        p.attachView(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        p.detachView()
    }

    override fun getCloseAccountSuccess(data: CloseAccountBean?) {

        val util = PrinterUtil(this)


        var cbPrintSettings1: Int = SPUtils.getInstance().getValue("cbPrintSettings0", 1) as Int
        var cbPrintSettings2: Int = SPUtils.getInstance().getValue("cbPrintSettings1", 1) as Int
        var cbPrintSettings3: Int = SPUtils.getInstance().getValue("cbPrintSettings2", 1) as Int
        var cbPrintSettings4: Int = SPUtils.getInstance().getValue("cbPrintSettings3", 1) as Int
        var cbPrintSettings5: Int = SPUtils.getInstance().getValue("cbPrintSettings4", 1) as Int
        var cbPrintSettings6: Int = SPUtils.getInstance().getValue("cbPrintSettings5", 1) as Int
        var cbPrintSettings7: Int = SPUtils.getInstance().getValue("cbPrintSettings6", 1) as Int
        var cbPrintSettings8: Int = SPUtils.getInstance().getValue("cbPrintSettings7", 1) as Int
        var cbPrintSettings9: Int = SPUtils.getInstance().getValue("cbPrintSettings8", 1) as Int

        var line = ArrayList<String> ()
        line.add("今日交班结算")
        line.add("门店名称："+data?.storeName)
        line.add("结算店员："+data?.storeUserName)

        if (cbPrintSettings1 == 2){
            line.add("交易单数："+data?.sumCount)
            line.add("交易金额："+data?.sumFee)
        }
        if (cbPrintSettings2 == 2){
            if(!TextUtils.isEmpty(data?.settlementFee) && !"null".equals(data?.settlementFee)){
                line.add("实收金额：" + data?.settlementFee)
            }else{
                line.add("实收金额：0" )

            }
        }
       if (cbPrintSettings4 == 2){
           if(!TextUtils.isEmpty(data?.refundCount.toString()) && !"null".equals(data?.refundCount)){
               line.add("退款单数：" + data?.refundCount)

           }else{
               line.add("退款单数：0" )


           }
           if(!TextUtils.isEmpty(data?.refundFee.toString()) && !"null".equals(data?.refundFee)){
               line.add("退款金额：" + data?.refundFee)

           }else{
               line.add("退款金额：0" )


           }
       }
        if (cbPrintSettings3 == 2){
            if(!TextUtils.isEmpty(data?.couponTotalFee) && !"null".equals(data?.couponTotalFee)){
              line.add("优惠金额：" + data?.couponTotalFee)

          }else{
              line.add("优惠金额：0" )

          }
      }
        if (cbPrintSettings6 == 2){

            if(!TextUtils.isEmpty(data?.depositCount.toString()) && !"null".equals(data?.depositCount)){
                line.add("押金单数：" + data?.depositCount)


            }else{
                line.add("押金单数：0" )

            }
            if(!TextUtils.isEmpty(data?.depositTotalFee) && !"null".equals(data?.depositTotalFee)){
                line.add("押金金额：" + data?.depositTotalFee)


            }else{
                line.add("押金金额：0" )

            }
        }
        if (cbPrintSettings7 == 2){
            line.add("微信单数："+data?.wxCount)
            line.add("微信金额："+data?.wxFee)
        }
        if (cbPrintSettings8 == 2){
            line.add("支付宝单数："+data?.aliCount)
            line.add("支付宝金额："+data?.aliFee)
        }
        if (cbPrintSettings5 == 2){
            line.add("会员单数："+data?.memberCount)
            line.add("会员金额："+data?.memberFee)
        }

        if (cbPrintSettings9 == 2){
            line.add("通用通单数："+data?.tytCount)
            line.add("通用通金额："+data?.tytFee)
        }
        if (!TextUtils.isEmpty(data?.beginTime) && !"null".equals(data?.beginTime)){
            line.add("开始时间："+data?.beginTime)
        }else{
            line.add("开始时间：")
        }
        line.add("结束时间："+data?.endTime+"\n\n\n\n\n")


        util.createData(line)

        var activity = Intent(this,LoginActivity :: class.java)
        startActivity(activity)
    }

    override fun getCloseAccountFailed(msg: String) {
        Toast.makeText(this,msg, Toast.LENGTH_SHORT)
    }

    override fun showLoadDialog() {
        loadDialog.show()
    }

    override fun dismissLoadDialog() {tvAccountPrintHistory
        loadDialog.dismiss()
    }

    override fun getLayoutId(): Int {
        return  R.layout.close_account_activity
    }


    var loadMore = true
    override fun initView() {




        viewClick()
        smartRefresh.setOnRefreshListener {
            loadMore = false
            pageNum = 1
            getList(1)
        }
        smartRefresh.setOnLoadMoreListener {
            loadMore = true
            pageNum++
            getList(pageNum)
        }


        getList(1)


    }


    var deviceNo = Build.SERIAL
    var pageNum = 1
    private fun viewClick() {

//        tvAccountPrintHistory.setOnClickListener {
//
//            getList(1)
//
//
//        }

        tvCloseAccount.setOnClickListener {
            p.getCloseAccount(deviceNo)
        }


        tvApBtn.setOnClickListener {

            finish()

        }
    }

    private fun getList(pageNum :Int) {
            val closeAccount =   mutableMapOf(
            Pair("deviceNo", deviceNo)
        )
        val data =  Gson().toJson(closeAccount)
        Logger.e(data.toString())

//            val map = mutableMapOf(
//                Pair("closeAccount",data)
//            )

        val map = mutableMapOf(
            Pair("deviceNo",deviceNo)
        )
        p.selectCloseAccountList(pageNum,20,map)

    }


}
